﻿using System;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Se.Framework.Common.Cache
{
    public class ReflectionCache<T>
            where T : class
    {
        private readonly ConditionalWeakTable<Type, T> cache;

        private readonly Func<Type, T> getValue;

        public ReflectionCache(Func<Type, T> getValue)
        {
            this.getValue = getValue;
            cache = new ConditionalWeakTable<Type, T>();
        }

        public T GetValue(Type type)
        {
            return cache.GetValue(type, (o) => this.getValue(o));
        }
    }
}
